package com.infore.airsupervise.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.infore.airsupervise.R;
import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.bean.StationDetailE;
import com.infore.airsupervise.biz.StationDetailBiz;
import com.infore.airsupervise.biz.impl.StationDetailBizImpl;
import com.infore.airsupervise.http.OnServerResponseListener;
import com.infore.airsupervise.ui.widget.SwipeRecyclerView;
import com.infore.airsupervise.ui.widget.refresh.swipetoloadlayout.OnLoadMoreListener;
import com.infore.airsupervise.ui.widget.refresh.swipetoloadlayout.OnRefreshListener;
import com.infore.airsupervise.utils.ImageUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 站点照片信息
 */
public class StationPhotoActivity extends BaseTitleBarActivity implements SwipeRecyclerView.OnActionListener {


    @BindView(R.id.refreshRecyclerView)
    SwipeRecyclerView recyclerView;
    private String mn;

    private BaseQuickAdapter<StationDetailE.SitePicture, BaseViewHolder> adapter;
    private List<Integer> imgList = new ArrayList<>();
    private List<StationDetailE.SitePicture> pictures = null;

    private StationDetailBiz stationBiz = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_station_photo);
        ButterKnife.bind(this);
        Intent it = getIntent();
        if (it != null) {
            mn = it.getStringExtra(Constants.INTENT_STR_KEY);
        }

        initViews();
    }

    private void initViews() {
        title.setText("照片信息");
        rightMenu.setVisibility(View.INVISIBLE);

        imgList.add(R.drawable.station_photo_01);
        imgList.add(R.drawable.station_photo_02);
        imgList.add(R.drawable.station_photo_03);
        imgList.add(R.drawable.station_photo_04);

        initRecycleView();

    }


    /**
     * 照片列表 设置
     */
    private void initRecycleView() {
        //recyleview 瀑布流
        recyclerView.getRecyclerView().setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        //适配器
        adapter = new BaseQuickAdapter<StationDetailE.SitePicture, BaseViewHolder>(R.layout.item_station_photo, pictures) {
            @Override
            protected void convert(BaseViewHolder helper, StationDetailE.SitePicture item) {
                ImageUtil.getInstance().displayImage((ImageView) helper.getView(R.id.iv_photo),item.getUrl(),200,200);

            }
        };
       recyclerView.setAdapter(adapter);
        recyclerView.setActionListener(this);
        adapter.setEnableLoadMore(false);
        // 首次自动刷新
        recyclerView.getSwipeLayout().setRefreshing(true);
        recyclerView.onRefresh();
    }

    /**
     * 获取图片url列表
     */
    private void getPictureData(){
        if(stationBiz == null){
            stationBiz = new StationDetailBizImpl();
        }
        stationBiz.sendHistoryPictureRequest(mn,"2017-05-20",responseListener);
    }

    /**
     * 请求回调方法
     */
    private OnServerResponseListener<List<StationDetailE.SitePicture>> responseListener = new OnServerResponseListener<List<StationDetailE.SitePicture>>() {
        @Override
        public void onSucceed(List<StationDetailE.SitePicture> result, String desc) {
            if(result == null){
                recyclerView.commonSuccessHandle(true, 0);
                return ;
            }
            recyclerView.commonSuccessHandle(true, result.size());
            adapter.getData().clear();
            adapter.addData(result);
        }

        @Override
        public void onFailed(int errorCode, String title, String desc) {
            recyclerView.commonFailedHandle(true,errorCode,title,desc);
        }
    };

    /**
     * 刷新加载
     */
    @Override
    public void onRefresh() {
        getPictureData();
    }

    @Override
    public void onLoadMore() {

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }
}
